package hx.front.manager.editor;

import hx.engine.IHouseEngine;
import hx.engine.ISectorEngine;
import hx.front.manager.ManagerAction;
import hx.model.dto.House;
import hx.model.dto.Sector;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 * Tareas de mantenimiento de una casa previamente filtrada. Alta,
 * edición y borrado.
 * 
 * @author kineas
 *
 */
public class HouseEditor extends ManagerAction {

	private static final long serialVersionUID = 1L;
	
	private Logger log = Logger.getLogger(HouseEditor.class);
	
	private IHouseEngine houseEngine;
	private ISectorEngine sectorEngine;
	
	private String id;
	private String name;
	private String alternateName;
	private Set<Sector> sectorList = new HashSet<Sector>();
	private Collection<Sector> sectorNotOwnedList = new HashSet<Sector>();
	
	
	@Override
	protected void fillForm(String id) {
		log.info(PRE_LOAD);
		
		
		
		log.info(POST_LOAD);
	}

	@Override
	protected void loadCollections() throws Exception {
		if (getId()!=null) {
			House aux = new House();
			aux.setId(getId());
			sectorNotOwnedList = sectorEngine.findByHouse(aux);
		}
		else sectorNotOwnedList = sectorEngine.findAll();
	}

	@Override
	public String updateCreate() throws Exception {
		House house = new House();
		house.setAlternateName(getAlternateName());
		house.setName(getName());
		house.setSectors(getSectorList());
		house.setId(getId());
		
		houseEngine.saveOrUpdate(house);
		return SUCCESS;
	}

	// GETTERS AND SETTERS
	public IHouseEngine getHouseEngine() {
		return houseEngine;
	}

	public void setHouseEngine(IHouseEngine houseEngine) {
		this.houseEngine = houseEngine;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAlternateName() {
		return alternateName;
	}

	public void setAlternateName(String alternateName) {
		this.alternateName = alternateName;
	}

	public ISectorEngine getSectorEngine() {
		return sectorEngine;
	}

	public void setSectorEngine(ISectorEngine sectorEngine) {
		this.sectorEngine = sectorEngine;
	}

	public Set<Sector> getSectorList() {
		return sectorList;
	}

	public void setSectorList(Set<Sector> sectorList) {
		this.sectorList = sectorList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Collection<Sector> getSectorNotOwnedList() {
		return sectorNotOwnedList;
	}

	public void setSectorNotOwnedList(Collection<Sector> sectorNotOwnedList) {
		this.sectorNotOwnedList = sectorNotOwnedList;
	}

}
